import {
  Component,
  Injector,
  ViewEncapsulation,
  ViewChild,
  ElementRef,
  OnInit
} from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
  ComboboxItemDto,
  SideTaskServiceProxy,
  ReceiveTaskInput
} from "@shared/service-proxies/service-proxies";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { Table } from "primeng/components/table/table";
import { Paginator } from "primeng/components/paginator/paginator";
import { LazyLoadEvent } from "primeng/components/common/lazyloadevent";
import * as moment from "moment";
import { finalize } from "rxjs/operators";
import { PutIntoProductionModalComponent } from "@app/plan/maintasks/put-into-production-modal.component";
import { MenuItem } from "primeng/api";

@Component({
  templateUrl: "./sidetask.component.html",
  styleUrls: ["./sidetask.component.less"],
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()]
})
export class SidetaskComponent extends AppComponentBase implements OnInit {
  // @ViewChild('boundingTeamGroupModal') boundingTeamGroupModal: BoundingTeamGroupModalComponent;
  @ViewChild("putIntoProductionModal")
  putIntoProductionModal: PutIntoProductionModalComponent;

  @ViewChild("dataTable") dataTable: Table;
  @ViewChild("paginator") paginator: Paginator;

  @ViewChild("dateTimePicker") dateTimePicker: ElementRef;

  @ViewChild("orderCategoryCombobox") orderCategoryCombobox: ElementRef;
  @ViewChild("statusCombobox") statusCombobox: ElementRef;
  @ViewChild("teamGroupCombobox") teamGroupCombobox: ElementRef;

  type = 1;
  statuses: ComboboxItemDto[];
  teamGroups: ComboboxItemDto[];
  en: any;
  filterText = "";
  advancedFiltersAreShown = false;
  isReceived = true;
  isCompleted = false;
  tabs: MenuItem[];
  loading: boolean;
  constructor(
    injector: Injector,
    private _sideTaskServiceProxy: SideTaskServiceProxy
  ) {
    super(injector);
  }

  ngOnInit() {
    this._sideTaskServiceProxy.getComboxItemsForSearch().subscribe(result => {
      this.statuses = result.statuses;
      this.teamGroups = result.teamGroups;
    });
    this.getTabs();
    this.loading = true;
    // this.en = {
    //   firstDayOfWeek: 0,
    //   monthNames: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
    //   monthNamesShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
    //   dateFormat: 'yy-mm',
    //   weekHeader: 'Wk'
    // };
  }
  getMonths(event?: LazyLoadEvent) {
    this.loading = true;
    this.primengTableHelper.showLoadingIndicator();
    this._sideTaskServiceProxy
      .getSideTasks(
        this.filterText,
        this.isReceived,
        undefined,
        this.isCompleted,
        53,
        this.primengTableHelper.getSkipCount(this.paginator, event),
        this.primengTableHelper.getMaxResultCount(this.paginator, event)
      )
      .subscribe(result => {
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.primengTableHelper.records = result.items;
        this.primengTableHelper.hideLoadingIndicator();
        setTimeout(() => {
          this.loading = false;
        }, 1000);
      });
  }
  receiveTask(id: number) {
    this.message.confirm("确认接收该条任务吗？", isConfirmed => {
      if (isConfirmed) {
        let rt = new ReceiveTaskInput();
        rt.id = id;
        rt.recipientUserId = 2;
        this._sideTaskServiceProxy.receiveTask(rt).subscribe(() => {
          this.notify.success("接收成功");
          this.getMonths();
        });
      }
    });
  }
  deleteModel(id: number) {
    this.message.confirm("确认删除该条型号计划吗？", isConfirmed => {
      if (isConfirmed) {
        this._sideTaskServiceProxy.deleteSideTask(id).subscribe(() => {
          this.notify.success(this.l("SuccessfullyDeleted"));
          this.getMonths();
        });
      }
    });
  }
  getTabs() {
    this.tabs = [
      { label: "已接收", icon: "fa fa-fw fa-bar-chart" },
      { label: "未接收", icon: "fa fa-fw fa-calendar" }
      // { label: '已完成', icon: 'fa fa-fw fa-calendar' }
    ];
  }
  tabTypeClick(tab: any) {
    this.isCompleted = false;
    this.isReceived = true;
    if (tab === "已接收") {
      this.type = 1;
      this.isReceived = true;
    } else if (tab === "未接收") {
      this.type = 2;
      this.isReceived = false;
    } else if (tab === "已完成") {
      this.isCompleted = true;
    }
    this.getMonths();
  }
  BatchReceiveTask() {
    this._sideTaskServiceProxy
      .getSideTasks(
        this.filterText,
        false,
        undefined,
        this.isCompleted,
        53,
        0,
        100000
      )
      .subscribe(result => {
        //console.log(result);
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.primengTableHelper.records = result.items;
        this.primengTableHelper.hideLoadingIndicator();
        let tt = result.items;
        tt.forEach(t => {
          let rt = new ReceiveTaskInput();
          rt.id = t.id;
          this._sideTaskServiceProxy.receiveTask(rt).subscribe(() => {});
        });
        this.getMonths();
      });
  }
}
